package com.tinckay.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.entity.po.Vehicle;
import com.tinckay.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Api(tags = "运输车辆管理")
@RestController
@RequestMapping("/vehicle")
public class VehicleController extends ApiController {

    @Autowired
    private VehicleService vehicleService;

    @Log("删除运输车辆信息")
    @DeleteMapping
    @ApiOperation("删除运输车辆信息")
    //@RequiresPermissions("vehicle:delete")
    public R delete(@ApiParam("运输车辆信息id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        vehicleService.removeByIds(CollectionUtil.toList(ids));
        return success(null);
    }

    @Log("新增运输车辆信息")
    @PostMapping
    @ApiOperation("新增运输车辆信息")
    @RequiresPermissions("vehicle:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) Vehicle vehicle) {
        return success(vehicleService.save(vehicle));
    }

    @Log("更新运输车辆信息")
    @PutMapping
    @ApiOperation("更新运输车辆信息")
    //@RequiresPermissions("vehicle:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) Vehicle vehicle) {
        vehicleService.updateById(vehicle);
        return success(vehicle);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询运输车辆信息列表")
    //@RequiresPermissions("vehicle:list")
    public R<IPage> list(@RequestParam(defaultValue = "1") int current,
                         @RequestParam(defaultValue = "10") int size) {
        IPage<Vehicle> page = vehicleService.page(new Page<>(current, size), null);
        return success(page);
    }

    @GetMapping("/info/{vehicleId}")
    @ApiOperation("获取运输车辆信息")
    //@RequiresPermissions("vehicle:info")
    public R<Vehicle> info(@PathVariable("vehicleId") long vehicleId) {
        return success(vehicleService.getById(vehicleId));
    }


}

